package com.yunshipei.ui.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.yunshipei.ui.view.client.H5WebChromeClient;
import com.yunshipei.ui.view.client.H5WebViewClient;

public class H5WebView extends WebView {

    public H5WebView(Context context) {
        super(context);
        privateInit(context);
    }

    public H5WebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        privateInit(context);
    }

    public H5WebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        privateInit(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public H5WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        privateInit(context);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void privateInit(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        WebSettings settings = getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDisplayZoomControls(false);
        setHorizontalScrollBarEnabled(false);
        setVerticalScrollBarEnabled(false);
        setWebViewClient(new H5WebViewClient());
        setWebChromeClient(new H5WebChromeClient());
    }
}
